//用户路由模块
const express = require("express");
const pool = require("../pool.js");
//创建路由器
const router = express.Router();
//暴露出去
module.exports = router;
//1.完成验证登录或密码是否存在
//get
//地址:http://127.0.0.1:3000/data/users/login
router.post("/login", (req, res, next) => {
  let phone = req.body.uphone;
  let pwd = req.body.upwd;
  let sql = "select * from f_users where uphone=?and upwd=?";
  pool.query(sql, [phone, pwd], (err, result) => {
    if (err) throw err;
    if (result[0]) {
      res.send({
        code: 201,
        msg: "登录成功",
      });
    } else {
      res.send({ code: 404, msg: "登录失败请检查用户名和密码" });
    }
  });
});

// 注册接口地址:http://127.0.0.1:3000/data/users/register
router.post("/register", (req, res, next) => {
  let name = req.body.uname;
  let pwd = req.body.upwd;
  let phone = req.body.uphone;
  // let avtar = req.body.uavtar;
  let province = req.body.uprovince;
  let city = req.body.ucity;

  let sql = "select uname,uphone from f_users where uname=? or uphone=?";
  let sql_zc =
    "INSERT INTO f_users(uname,upwd,uphone,uprovince,ucity) VALUES(?,?,?,?,?)";
  pool.query(sql, [name, phone], (err, result) => {
    if (err) throw err;
    if (result[0]) {
      res.send({
        code: 406,
        msg: "用户已存在或手机号已存在",
      });
    } else {
      pool.query(sql_zc, [name, pwd, phone, province, city], (err, result) => {
        if (err) throw err;
        if (result.affectedRows == 1) {
          res.send({
            code: 201,
            msg: "注册成功",
            data: result,
          });
        }
      });
    }
  });
});

//用户信息接口地址:http://127.0.0.1:3000/data/users/rootuser
router.get("/rootuser", (req, res, next) => {
  let sql = "select * from f_users ";
  pool.query(sql, (err, result) => {
    res.send({
      data: result,
    });
  });
});

//用户删除接口地址:http://127.0.0.1:3000/data/users/rootdelete
// router.get("/rootdelete", (req, res, next) => {
//   let sql = "delete  from f_users  whrer=?";
//   pool.query(sql, (err, result) => {
//     res.send({ data: result });
//   });
// });

//修改用户名http://127.0.0.1:3000/data/users/mod_name

//修改用户数据接口地址:http://127.0.0.1:3000/data/users/update
router.post("/update", (req, res, next) => {
  let name = req.body.uname;
  let pwd = req.body.upwd;
  let phone = req.body.uphone;
  // let sql = `SELECT name FROM f_users WHERE uname="${name}";`
  let sql = `UPDATE f_users SET upwd=?,uphone=?  WHERE uname=?; `;
  pool.query(sql, [pwd, phone, name], (err, result) => {
    if (err) throw err;
    if (result.changedRows == 1) {
      res.send({
        code: 201,
        msg: "修改成功",
      });
    } else {
      res.send({
        code: 406,
        msg: "修改失败",
      });
    }
  });
});

//判断密码是否相同接口地址:http://127.0.0.1:3000/data/users/something
router.post("/something", (req, res, next) => {
  let pwd = req.body.upwd;
  let phone = req.body.uphone;
  let sql = `SELECT uid FROM f_users WHERE upwd=? and uphone=?;`;
  pool.query(sql, [pwd, phone], (err, result) => {
    if (err) throw err;
    if (result[0]) {
      res.send({
        code: 201,
        msg: "密码验证正确",
        data: result,
      });
    } else {
      res.send({
        code: 406,
        msg: "密码错误",
      });
    }
  });
});

//判断手机号码是否存在:http://127.0.0.1:3000/data/users/somephone
router.post("/somephone", (req, res, next) => {
  let phone = req.body.uphone;
  let sql = "SELECT * FROM f_users WHERE uphone=?";
  pool.query(sql, [phone], (err, result) => {
    if (err) throw err;
    if (!result[0]) {
      res.send({
        code: 201,
        msg: "该手机号码可以注册",
        data: result,
      });
    } else {
      res.send({
        code: 406,
        msg: "该手机号码已经存在",
      });
    }
  });
});

//通过手机号码查询uname:http://127.0.0.1:3000/data/users/uname
router.post("/uname", (req, res, next) => {
  let phone = req.body.uphone;
  let sql = `SELECT uname FROM f_users WHERE uphone=?;`;
  pool.query(sql, [phone], (err, result) => {
    if (err) throw err;
    if (result[0]) {
      res.send({
        code: 201,
        msg: "用户名已查到",
        data: result,
      });
    } else {
      res.send({
        code: 406,
        msg: "查找失败",
      });
    }
  });
});

// 2.登录查询名字或手机号是否存在
//post
//地址:http://127.0.0.1:3000/data/users/check_uname?uname=
//router.get("/check_uname", (req, res) => {
// let name = req.query.uname;
// if (!name) {
// res.send("用户名/手机号不能为空");
// return;
//}
//let sql = "select uid from fy_users where uname=? || uphone=?";
//pool.query(sql, [name, name], (err, result) => {
// if (err) throw err;
// if (result[0]) {
//   res.send({ code: 201, msg: "" });
//  } else {
//   res.send({ code: 105, msg: "用户名不存在" });
// }

//});
